package com.wan.core.timer.task;

import cn.hutool.log.StaticLog;
import com.wan.core.timer.bucket.Bucket;

/**
 * 任务的双向链表
 * @author wwh
 * @date 2022/6/28 16:42
 */
public class TaskLinked {
    /** 上一个节点 */
    public TaskLinked prev;
    /** 下一个节点 */
    public TaskLinked next;
    /** 任务 */
    public TaskNode taskNode;
    /** 所在的槽位 */
    public Bucket bucket;

    public TaskLinked(TaskNode taskNode,Bucket bucket) {
        this.taskNode = taskNode;
        this.bucket = bucket;
    }

    /**
     * 插入到该节点后面
     */
    public void postInsert(TaskLinked taskLinked){
        taskLinked.prev = this;
        if(next != null){
            next.prev = taskLinked;
        }
        taskLinked.next = next;
        next = taskLinked;
    }

    /**
     * 插入到该节点前面
     */
    public void preInsert(TaskLinked taskLinked){
        taskLinked.next = this;
        if(prev != null){
            prev.next = taskLinked;
        }
        taskLinked.prev = prev;
        prev = taskLinked;
    }

    /**
     * 删除自己
     */
    public void removeSelf(){
        if(prev != null){
            prev.next = next;
        }
        if(next != null){
            next.prev = prev;
        }
        this.prev = null;
        this.next = null;
        this.taskNode = null;
        this.bucket = null;

    }


}
